package Action;


import java.util.UUID;

import Aptech.Common.Formater;
import Aptech.Service.SV_Buitls;
import Aptech.Service.SV_Rooms;
import Form.BuiltsForm;
import Form.RoomForm;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.Rooms;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


public class RoomAction extends DispatchAction{
	private SV_Buitls mgBuilt = new SV_Buitls();
	private SV_Rooms mgRoom = new SV_Rooms();
	private RoomForm roomForm;
	
	 public ActionForward addRoom(ActionMapping map, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response)
	            throws Exception {
		 		 
		 roomForm = (RoomForm) form;
     	request.setAttribute("arrBuilt", mgBuilt.getAllBuilt());
     	request.setAttribute("sitemap", "Add A New Room");
         String type = request.getParameter("type");
         if (type == null) {
             request.setAttribute("type", "add");
             return map.findForward("edit");
         } else {
             UUID id = UUID.randomUUID();
             Rooms room = new Rooms();
             room.setName(roomForm.getName());
             room.setFloo(roomForm.getFloo());
             room.setIdBuilt(roomForm.getIdBuilt());
             room.setId(id.toString());              
             room.setNote(roomForm.getNote());
             room.setPrice(roomForm.getPrice());
             room.setSquare(roomForm.getSquare());
             room.setStatus(roomForm.getStatus());
           
             
             mgRoom.addRoom(room);
             request.setAttribute("save", "save");
         }

         return map.findForward("edit");
	 }
	 
	 public ActionForward editRoom(ActionMapping map, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response)
	            throws Exception {
		 roomForm = (RoomForm) form;
     	String id = request.getParameter("id");
     	request.setAttribute("sitemap", "Edit of A Built");
     	String type = request.getParameter("type");
     	if (type.equals("edit1")) {
             request.setAttribute("id", id);
             request.setAttribute("type", type);
             request.setAttribute("arrBuilt", mgBuilt.getAllBuilt());
             // Set value for Form
             Rooms room = mgRoom.getRoomByID(id);
          
             roomForm.setName(room.getName());
             roomForm.setFloo(room.getFloo());
             roomForm.setIdBuilt(room.getIdBuilt());
             roomForm.setId(room.getId());              
             roomForm.setNote(room.getNote());
             roomForm.setPrice(room.getPrice());
             roomForm.setSquare(room.getSquare());
             roomForm.setStatus(room.getStatus());

            
         } else {
        	 request.setAttribute("arrBuilt", mgBuilt.getAllBuilt());
         	 Rooms room = new Rooms();
         	room.setName(roomForm.getName());
            room.setFloo(roomForm.getFloo());
            room.setIdBuilt(roomForm.getIdBuilt());
            room.setId(id.toString());              
            room.setNote(roomForm.getNote());
            room.setPrice(roomForm.getPrice());
            room.setSquare(roomForm.getSquare());
            room.setStatus(roomForm.getStatus());            
            mgRoom.editRoom(room);
            request.setAttribute("save", "save");
         }
		 
		 return map.findForward("edit");
	 }

	 public ActionForward getAllRoom(ActionMapping map, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response)
	            throws Exception {
		 
		 roomForm = (RoomForm) form;
		 roomForm.setArrRooms(mgRoom.getAllRoom());
		 request.setAttribute("sitemap", "List of Rom");
		 return map.findForward("list");
	 }

	 public ActionForward deleteRoom(ActionMapping map, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response)
	            throws Exception {
		 roomForm = (RoomForm) form;
		 String id = request.getParameter("id");
		 mgRoom.deleteRoom(id);
		 response.sendRedirect("/NTB/room.do?method=getAllRoom");
		 return map.findForward("list");
		 
	 }
	 
	 public ActionForward detailRoom(ActionMapping map, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response)
	            throws Exception {
		 
		 roomForm = (RoomForm) form;
         String id = request.getParameter("id");
         request.setAttribute("id", id);
         request.setAttribute("type", "detail");
         request.setAttribute("arrBuilt", mgBuilt.getAllBuilt());
         request.setAttribute("sitemap", "Information Detail Of A Room");
             // Set value for Formpe
             Rooms room = mgRoom.getRoomByID(id);
             roomForm.setName(room.getName());
             roomForm.setFloo(room.getFloo());
             roomForm.setIdBuilt(room.getIdBuilt());
             roomForm.setId(room.getId());              
             roomForm.setNote(room.getNote());
             roomForm.setPrice(room.getPrice());
             roomForm.setSquare(room.getSquare());
             roomForm.setStatus(room.getStatus());
		 
		 return map.findForward("edit");
	 }
	 
	 public ActionForward findRoom(ActionMapping map, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response)
	            throws Exception {
		 
		 roomForm = (RoomForm) form;
		 request.setAttribute("arrBuilt", mgBuilt.getAllBuilt());
		 String idBuilt = request.getParameter("idBuilt");
		 String type = request.getParameter("type");
		 
		 if(type == null){
			 if(mgRoom.getAllRoom() != null){
				 roomForm.setArrRooms(mgRoom.getAllRoom());		
			 }
			  		 			
		 }else{
			 if(mgRoom.getRoomByBuilt(idBuilt) != null){
				 roomForm.setArrRooms(mgRoom.getRoomByBuilt(idBuilt));
			 }
		 	
		 }
		 return map.findForward("findroom");

	 }
}
